package cn.hx.whh.automaticticket.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import cn.hx.whh.automaticticket.R;

public class QrCodeIndexActivity extends Activity {
    private Timer timer;
    private TimerTask task;
    private TextView bottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_code_index);
        this.timer = new Timer(true);
        this.task = new TimerTask() {
            public void run() {
                switchActivity();
            }
        };
        bottom = (TextView) findViewById(R.id.bottom);
        bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                automaticPrint(view);
            }
        });
        init();
    }

    private void init() {
        timer.schedule(task, 1000 * 60 * 3);
    }


    public void automaticPrint(View view) {
        startActivity(new Intent(this, AutomaticTicketActivity.class));
    }

    private void switchActivity() {
        startActivity(new Intent(this, IndexActivity.class));
    }

    private void closeTimer() {
        if (this.timer != null) {
            this.timer.cancel();
            this.timer = null;
        }
        if (this.task != null) {
            this.task.cancel();
            this.task = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeTimer();
    }

    @Override
    public void onStop() {
        super.onStop();
        closeTimer();
    }
}